<template>
  <div>
    <el-table
      :data="shopList"
      style="width: 90%"
      height="600"
      empty-text="当前没有数据"
      align="center"
    >
      <el-table-column label="基本信息" width="200" align="center">
        <template #default="scope">
          <img
            style="width: 88px; height: 88px"
            :src="scope.row.ImgUrl"
            alt=""
          />
          <!-- <div style="margin-left: 10px"></div> -->
          <div
            :contenteditable="scope.row.audit_status == 0"
            @blur="blurInput(scope.row)"
            @input="inputDiv($event)"
          >
            {{ scope.row.Name }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="价格" width="250" align="center">
        <template #default="scope">
          <!-- <i class="el-icon-time"></i> -->
          <div style="margin-left: 10px" v-if="scope.row.PriceType == 1">
            现价：￥{{ scope.row.Price }}
          </div>
          <span style="margin-left: 10px" v-if="scope.row.PriceType == 2"
            >￥{{ scope.row.Price + '——' + '￥' + scope.row.Price2 }}
          </span>
          <span style="margin-left: 10px" v-if="scope.row.PriceType == 3"
            >{{
              '市场价：￥' +
              scope.row.Price +
              '—' +
              '现价：￥' +
              scope.row.Price2
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="链接" width="200" align="center">
        <template #default="scope">
          <!-- <i class="el-icon-time"></i> -->
          <span style="margin-left: 10px">{{ scope.row.Url }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="200" align="center">
        <template #default="scope">
          <!-- <i class="el-icon-time"></i> -->
          <span style="margin-left: 10px">{{
            audit_status(scope.row.audit_status)
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        #default="scope"
        fixed="right"
        label="操作"
        width="300"
        align="center"
      >
        <el-button
          :disabled="scope.row.audit_status == 1"
          size="mini"
          type="danger"
          @click="handleDelete(scope.row)"
          >删除</el-button
        >
        <el-button
          :disabled="scope.row.audit_status == 1 || scope.row.audit_status == 3"
          size="mini"
          type="primary"
          @click="Submit(scope.row)"
          >提交审核</el-button
        >
        <el-button
          :disabled="scope.row.audit_status == 1 || scope.row.audit_status == 3"
          size="mini"
          type="primary"
          @click="handleModify(scope.row)"
          >修改价格</el-button
        >
      </el-table-column>
    </el-table>
    <div class="block-pagination">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        v-model:currentPage="currentPage"
        :page-size="100"
        layout="prev, pager, next, jumper"
        :total="totalPage"
      >
      </el-pagination>
    </div>
    <ShopDialog :Modify="Modify" ref="ShopDialog"></ShopDialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, inject, onMounted, reactive, ref, watch } from 'vue'
import {
  NotWarehoused,
  goShopDelete,
  SubmitProductAudit,
  UpdateNotWarehoused,
} from '@/service/main/main'
import { ElMessageBox, ElMessage } from 'element-plus'
import ShopDialog from './Dialog.vue'

export default defineComponent({
  props: ['searchInput'],
  components: { ShopDialog },
  async setup(props) {
    let ShopDialog = ref()
    onMounted(() => {
      return ShopDialog
    })
    watch(
      () => props.searchInput,
      (newValue, oldValue) => {
        console.log({ newValue, oldValue })
        searchInputProps = newValue
        goList(newValue)
      },
    )
    const FormData = reactive({
      price: '',
    })
    // 刷新页面
    const reload: any = inject('reload')

    // 接受input传输值
    let searchInputProps = ''
    let shopRes = ref()
    let totalPage = ref()
    // 分页
    const currentPage = ref(1)
    // 获取商品列表
    let shopList = ref()
    // 每页页数
    const pages = 10

    const goList = async (searchInputProps: any) => {
      // 分页
      shopRes.value = await NotWarehoused(
        searchInputProps,
        currentPage.value - 1,
        pages,
      )
      totalPage.value = Math.ceil(shopRes.value.Total / pages) * 100
      shopList.value = shopRes.value.List
      console.log(totalPage)
    }
    goList(searchInputProps)

    // 分页事件
    const handleCurrentChange = async (val: any) => {
      currentPage.value = val
      let shopRes = await NotWarehoused(
        searchInputProps,
        currentPage.value - 1,
        pages,
      )
      shopList.value = shopRes.List
      // console.log(shopList.value)
    }
    // 删除
    const open = (goodsId: number | string) => {
      ElMessageBox.confirm('此操作将永久删除商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          goShopDelete(goodsId)
          ElMessage({
            type: 'success',
            message: '删除成功!',
          })
          reload()
        })
        .catch(() => {
          ElMessage({
            type: 'info',
            message: '已取消删除',
          })
        })
    }
    // 删除
    const handleDelete = (row: any) => {
      open(row.GoodsId)
    }

    // 提交审核
    const Submit = async (item: any) => {
      console.log(item)
      let arraySubmit: any[] = []
      arraySubmit.push(item.Id)
      const resSubmit = await SubmitProductAudit(arraySubmit, item.PriceType)
      console.log(resSubmit)
      reload()
    }

    // 打开价格修改
    let Modify = ref()
    const handleModify = (item: any) => {
      ShopDialog.value.dialogFormVisible = true
      Modify.value = item
      // Modify.Id = item.Id
      // Modify.ImgUrl = item.ImgUrl
      // Modify.Price = item.PriceType
      // Modify.Price2 = item.PriceType
      // Modify.Name = item.PriceType
    }
    const audit_status = (params: any) => {
      if (params == 0) {
        return '未审核'
      }
      if (params == 1) {
        return '审核中'
      }
      if (params == 2) {
        return '审核通过'
      }
      if (params == 3) {
        return '审核失败'
      }
    }

    const divMsg = ref('')
    const inputDiv = (event: any) => {
      console.log(event.target.innerHTML)
      divMsg.value = event.target.innerHTML
    }
    const blurInput = (item: {
      Id: string | number
      ImgUrl: string | number
      PriceType: string | number
      Price: string | number
      Price2: string | number
      Name: string | number
    }) => {
      if (divMsg.value == '0') {
        ElMessage({
          type: 'warning',
          message: '名称不能为空',
        })

        return false
      }

      modify(
        item.Id,
        item.ImgUrl,
        item.PriceType,
        item.Price,
        item.Price2,
        divMsg.value,
      )
    }
    const modify = (
      Id: string | number,
      ImgUrl: string | number,
      PriceType: string | number,
      Price: string | number,
      Price2: string | number,
      Name: string | number,
    ) => {
      UpdateNotWarehoused(Id, ImgUrl, PriceType, Price, Price2, Name)
      divMsg.value = ''
    }
    return {
      shopRes,
      shopList,
      currentPage,
      totalPage,
      searchInputProps,
      FormData,
      divMsg,
      ShopDialog,
      Modify,
      goList,
      handleCurrentChange,
      handleDelete,
      audit_status,
      Submit,
      inputDiv,
      blurInput,
      modify,
      handleModify,
    }
  },
})
</script>

<style scoped>
.block-pagination {
  padding: 10px 20px;
}
</style>
